<?php



/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Story
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */



class Story_Form_Create_Custom extends Engine_Form
{
	public $_error = array();

	private $_category_id;
	private $_category_title;	
	
	const SHORT_STORY = 1;
	const NOVEL = 2;
	const COMICS = 3;
	const PHOTO_STORY = 4;
	const SCRIPT_PLAY = 5;
	const ARTICLE = 6;
	const STORY_THREAD = 7;
	
	private $_body_labels = array(
			Story_Model_Category::SHORT_STORY => 'Short Story Content',
			Story_Model_Category::NOVEL => 'Chapter Content',
			Story_Model_Category::COMICS => 'Story Body',
			Story_Model_Category::PHOTO_STORY => 'Story Body',
			Story_Model_Category::SCRIPT_PLAY => 'Content of Poem',
			Story_Model_Category::ARTICLE => 'Content of Article',
			Story_Model_Category::STORY_THREAD => 'Story Body',
			);

	private $_title_labels = array(
			Story_Model_Category::SHORT_STORY => 'Short Story Title',
			Story_Model_Category::NOVEL => 'Chapter Title',
			Story_Model_Category::COMICS => 'Story Title',
			Story_Model_Category::PHOTO_STORY => 'Title',
			Story_Model_Category::SCRIPT_PLAY => 'Title of Poem',
			Story_Model_Category::ARTICLE => 'Title of Article',
			Story_Model_Category::STORY_THREAD => 'Story Title',
	);
	
	public function setTit($category_title){
		$this->_category_title = $category_title;
	}

	public function getTit(){
		return $this->_category_title;
	}

	public function setCategory($category_id){
		$this->_category_id = $category_id;
	}

	public function getCategory(){
		return $this->_category_id;
	}

	public function init()
	{
		$title = $this->_title_labels[$this->_category_id];		
		$this->addElement('Text', 'title', array(
				'label' => $title,
				'allowEmpty' => false,
				'required' => true,
				'filters' => array(
						'StripTags',
						new Engine_Filter_Censor(),
						new Engine_Filter_StringLength(array('max' => '127')),
				)));
		
		
		
		if($this->_category_id == Story_Model_Category::ARTICLE){	
			
			$date_label = "selected_date_" . $this->_category_id;
			$end_cal = new Engine_Form_Element_CalendarDateTime($date_label);
			$end_cal->setLabel("Date and Time");
			$end_cal->setValue(date('Y-m-d H:i:s'));
			
			$this->addElement($end_cal);			
		}

		if($this->_category_id == Story_Model_Category::ARTICLE || $this->_category_id == Story_Model_Category::PHOTO_STORY){
		
			$this->addElement('Text', 'location', array(
					'label' => 'Location',
					'filters' => array(
							'StripTags',
							new Engine_Filter_Censor(),
							new Engine_Filter_StringLength(array('max' => '127')),
					)));	
		
		}
		
		if($this->_category_id == Story_Model_Category::PHOTO_STORY){
			// Description
			$this->addElement('Textarea', 'description', array(
					'label' => 'Short Description',
			));
			$this->description->getDecorator("Description")->setOption("placement", "prepend");
		}
		
		

		$user = Engine_Api::_()->user()->getViewer();
		$user_level = Engine_Api::_()->user()->getViewer()->level_id;

		$body_element = "body_" . $this->_category_id;		

		if($this->_category_id != Story_Model_Category::PHOTO_STORY){
			$label = $this->_body_labels[$this->_category_id];
			$this->addElement('TinyMce', $body_element, array(
					'label' => $label,
					'required' => true,
					'allowEmpty' => false,
					'filters' => array(
							new Engine_Filter_Censor(),
					),
					'editorOptions' => array(
							//'upload_url' => $upload_url,
							'remove_script_host' => '',
							'convert_urls' => '',
							'relative_urls' => '',
							'mode' => 'exact',
							'elements' => $body_element,
							'width' => 500,
							'height' => 420,
							'media_strict' => false,
							'extended_valid_elements' => '*[*],**,object[width|height|classid|codebase|id|name],param[name|value],embed[src|type|width|height|flashvars|wmode|id|name],iframe[src|style|width|height|scrolling|marginwidth|marginheight|frameborder|id|name|class],video[src|type|width|height|flashvars|wmode|class|poster|preload|id|name],source[src]',
							'plugins' => "emotions, table, fullscreen, preview, paste, style, layer, xhtmlxtras",
							'theme_advanced_buttons1' => "cut,copy,paste,pastetext,pasteword,|,undo,redo,|,link,unlink,anchor,charmap,image,media,|,justifyleft,justifycenter,justifyright,justifyfull,|,hr,removeformat,code,preview",
							'theme_advanced_buttons2' => "bold,italic,underline,strikethrough,|,bullist,numlist,|,outdent,indent,|,tablecontrols",
							'theme_advanced_buttons3' => "formatselect,fontselect,fontsizeselect,|,forecolor,backcolor,|,styleprops,attribs,|,blockquote,sub,sup",
					)
			));
		}
		
		if($this->_category_id == Story_Model_Category::ARTICLE 
				|| $this->_category_id == Story_Model_Category::NOVEL 
				||  $this->_category_id == Story_Model_Category::SHORT_STORY
				||  $this->_category_id == Story_Model_Category::SCRIPT_PLAY ){
			$file_upload = 'file_' . $this->_category_id;
			$this->addElement('File', $file_upload , array(
					'label' => 'Upload Content',
					'description' => 'You can upload content a txt file. Once uploaded, it will go directly to the text area of the story.'
			));
		}
		
		$photo_label = "Cover Photo";
		if($this->_category_id == Story_Model_Category::PHOTO_STORY){
			$photo_label = "Image for Story";
		}
		$this->addElement('File', 'photo', array(
				'label' => $photo_label,
				'description' => 'Please upload your Cover Image for Search and Browse Story'
		));
		
		if($this->_category_id == Story_Model_Category::PHOTO_STORY){
			//Description
			$this->addElement('Textarea', 'photo_description', array(
					'label' => 'Image Description',
			));
			$this->description->getDecorator("Description")->setOption("placement", "prepend");
		}

		$this->addElement('Hidden', 'category_id', array(
				'validators' => array(
						'Int',
				),
				'order' => '10'
		));
		$this->category_id->setValue($this->_category_id);

		//Add hidden element for tags, published, auth_view, auth_comment
		$this->addElement('Hidden', 'tags', array('order' => '11'));
		//$this->addElement('Hidden', 'published', array('order' => '12'));
		$this->addElement('Hidden', 'story_id', array('order' => '12'));
		$this->addElement('Hidden', 'auth_view', array('order' => '13'));
		$this->addElement('Hidden', 'auth_comment', array('order' => '14'));
		$this->addElement('Hidden', 'story_category', array('order' => '15'));

		$this->addElement('Checkbox', 'agree', array(
				'label' => 'I accept the term and privacy of story creation',
				'value' => True
		));

		// Buttons
		$this->addElement('button', 'submit', array(
				'label' => 'Save',
				'type' => 'submit',
				'value' => 'save',
				'ignore' => true,
				'decorators' => array('ViewHelper')
		));
		
		if($this->_category_id == Story_Model_Category::SHORT_STORY ||
			$this->_category_id == Story_Model_Category::ARTICLE ||
			$this->_category_id == Story_Model_Category::PHOTO_STORY ||
			$this->_category_id == Story_Model_Category::SCRIPT_PLAY ||
			$this->_category_id == Story_Model_Category::NOVEL){
			$this->addElement('button', 'publish', array(
					'label' => 'Save & Publish',
					'type' => 'submit',
					'value' => 'publish',
					'ignore' => true,
					'decorators' => array('ViewHelper')
			));
		}		
		

		$this->addElement('Cancel', 'cancel', array(
				'label' => 'cancel',
				'link' => true,
				'prependText' => ' or ',
				'decorators' => array(
						'ViewHelper'
				)
		));

		if($this->_category_id == Story_Model_Category::SHORT_STORY ||
			$this->_category_id == Story_Model_Category::ARTICLE ||
			$this->_category_id == Story_Model_Category::PHOTO_STORY ||
			$this->_category_id == Story_Model_Category::SCRIPT_PLAY ||
			$this->_category_id == Story_Model_Category::NOVEL){
			$this->addDisplayGroup(array('submit', 'publish', 'cancel'), 'buttons');
		} else {
			$this->addDisplayGroup(array('submit', 'cancel'), 'buttons');
		}
		
		
		$button_group = $this->getDisplayGroup('buttons');
		$button_group->addDecorator('DivDivDivWrapper');

		$form_id = "story_create_" . $this->_category_id;

		$this->addAttribs(array('onsubmit' => 'return en4.story.handlesubmit(this);', 'id' => $form_id));
		//Set Action
		$this->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action'=>'createex'),'story_general',true));
	}

}